# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#
# Testing
# RoleDef/Role and Dependencies
#              Proxy
#              /  |
#           Head  |            
#             \   |
#            Workers (2 of them)
#
# VMs
# VM0: Proxy+Head
# VM1: Worker1
# VM2: Worker2
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

from crv.model import *
from crv.tools.crvtools import buildRoleDep



def createClusterModels(metadata,session,config):
    if not config.has_section( "cluster_model_analysis" ):
        raise RuntimeError("Missing configuration for cluster_model_analysis")
    
    
    vm_maker=createVMMaker(config)
    rp=createAnalysisResourcePoolEC2(metadata,session,vm_maker)
    createAnalysisCluster(metadata,session,rp,config)
    
def createVMMaker(config):
    
    vm_maker=EucaVMMaker("ec2")
    
    vm_maker.setAttr('EC2_URL', config.get("cluster_model_analysis","ec2_url"))
    vm_maker.setAttr('EC2_ACCESS_KEY', config.get("cluster_model_analysis","ec2_access_key"))
    vm_maker.setAttr('EC2_SECRET_KEY',config.get("cluster_model_analysis","ec2_secret_key"))
    
    vm_maker.setAttr('image_id', config.get("cluster_model_analysis","image_id")) 
    vm_maker.setAttr('keyname', config.get("cluster_model_analysis","keyname"))
    vm_maker.setAttr('kernel_id', config.get("cluster_model_analysis","kernel_id"))
    vm_maker.setAttr('ramdisk_id', config.get("cluster_model_analysis","ramdisk_id"))
    vm_maker.setAttr('instance_type', config.get("cluster_model_analysis","instance_type") )
    vm_maker.setAttr('group_names', config.get("cluster_model_analysis","group_names") )
    vm_maker.setAttr('addressing_type', config.get("cluster_model_analysis","addressing_type"))
    
    return vm_maker


def createAnalysisResourcePoolEC2(metadata,session,vm_maker):
    
    #Resource Pool

    cpc=CentOS5_Puppet_ClientMaker(name="RHEL5_Puppet",doc="CRVClient for RHEL5 Clients using Puppet",templatedir="/opt/cloudcrv/cloudcrv/crvclient")
    ppm=Puppet_ProfileMaker(name="Puppet",doc="Profile Maker using Puppet")

    rp=RP("publicIP","Resource Pool with Publicly Addressed VMs, Puppet Profiles and RHEL5 Clients",profilemaker=ppm,vmmaker=vm_maker,clientmaker=cpc)

    session.add(rp)

    return rp


def createAnalysisCluster(metadata,session,rp,config):
    
    condor_password=config.get("cluster_model_analysis","condor_password")
    ldap_password=config.get("cluster_model_analysis","ldap_password")
    num_workers=int(config.get("cluster_model_analysis","num_workers"))
    num_interactive=int(config.get("cluster_model_analysis","num_interactives"))

    #Role Definitions
    rdLDAPSrv=RoleDef('LDAPSrv',"LDAP Server","at3_ldapserver","puppet")
    
    rdLDAPClient=RoleDef('LDAPClient',"LDAP Linux Auth Client","at3_ldapclient","puppet")
    rdLDAPClient.addDependOn(rdLDAPSrv)
    
    rdNFSSrv=RoleDef('NFSSrv',"NFS Server","at3_nfssrv","puppet")
    
    
    rdNFSClient=RoleDef('NFSClient',"NFS Client","at3_nfsclient","puppet")
    rdNFSClient.addDependOn(rdNFSSrv)

    rdProxy=RoleDef('Proxy',"Proxy Server","at3_proxy","puppet")
#    rdProxy.addDependOn(rdNFSClient,local=True)
    
    rdCVMFSClient=RoleDef('CVMFSClient',"CVMFS Client","at3_cvmfs","puppet")
    rdCVMFSClient.addDependOn(rdProxy)

    rdHead=RoleDef('Head',"Condor Head","at3_condor","puppet")
    rdHead.addDependOn(rdLDAPClient,local=True)
    rdHead.addDependOn(rdNFSClient,local=True)
    rdHead.addDependOn(rdProxy)

    rdInteractive=RoleDef('Interactive',"Condor Interactive","at3_condor","puppet")
    rdInteractive.addDependOn(rdLDAPClient,local=True)    
    rdInteractive.addDependOn(rdNFSClient,local=True)
    rdInteractive.addDependOn(rdCVMFSClient,local=True)
    rdInteractive.addDependOn(rdHead)

    rdWorker=RoleDef('Worker',"Condor Worker","at3_condor","puppet")
    rdWorker.addDependOn(rdLDAPClient,local=True)    
    rdWorker.addDependOn(rdNFSClient,local=True)    
    rdWorker.addDependOn(rdCVMFSClient,local=True)
    rdWorker.addDependOn(rdHead)
    
    cl=Cluster("tier3 analysis cluster")
    cl.setAttr("confdir","/opt/cloudcrv/puppet")


    rLDAPSrv=cl.addRole(name="LDAP", roledef=rdLDAPSrv, vm=rp.newVM("LDAP Server"),enabled=True)
    rLDAPSrv.setAttr("rootpw",ldap_password)
    rLDAPSrv.setAttr("suffix","dc=mytier3,dc=com")
    
    
    rNFSSrv=cl.addRole(name="NFS", roledef=rdNFSSrv, vm=rp.newVM("NFS Server"), enabled=True)
    rNFSSrv.setAttr("NFSServerSharePath","/mnt/export/home")
    rNFSSrv.setAttr("NFSServerClientMountPath","/export/home")


    rProxy=cl.addRole(name="Proxy", roledef=rdProxy, vm=rp.newVM("Squid Proxy"), enabled=True)

    rHead=cl.addRole(name="Head",roledef=rdHead,vm=rp.newVM("Condor Head"),enabled=True)
    rHead.setAttr("CondorRoleName","head")
    rHead.setAttr("CondorAllowNegotiatorExtra","")
    rHead.setAttr("CondorFilesystemDomain","atlastier3")
    rHead.setAttr("CondorPassword",condor_password)
    
    for i in range(num_interactive):
        rr=cl.addRole(name="Interactive%02d"%i,roledef=rdInteractive,vm=rp.newVM("Interactive%02d"%i),enabled=True)
        rr.setAttr("CondorRoleName","interactive")
        rr.setAttr("CondorAllowNegotiatorExtra","")
        rr.setAttr("CondorFilesystemDomain","atlastier3")
        rr.setAttr("CondorPassword",condor_password)

    for i in range(num_workers):
        rr=cl.addRole(name="Worker%03d"%i, roledef=rdWorker, vm=rp.newVM("Worker%03d"%i), enabled=True)
        rr.setAttr("CondorRoleName","worker")
        rr.setAttr("CondorAllowNegotiatorExtra","")
        rr.setAttr("CondorFilesystemDomain","atlastier3")
        rr.setAttr("CondorPassword",condor_password)               
    
    cl.targetSC=Cluster.SC.SC_INITIALIZED

    buildRoleDep(cl.role)

    session.add(cl)
    
